package com.example.myapp.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapp.ADO.FriendAapter;
import com.example.myapp.Loading.ShapeLoadingDialog;
import com.example.myapp.Model.User;
import com.example.myapp.R;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class Myguanzhu extends AppCompatActivity {
    private ListView lsitnew;
    private ShapeLoadingDialog shapeLoadingDialog;
    private  User user = BmobUser.getCurrentUser(User.class);//查自己

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);  //隐藏标题栏
        setContentView(R.layout.myguanzhu);
        lsitnew=findViewById(R.id.myguanzhulistview);
        ImageView myguanzhu_back=findViewById(R.id.myguanzhu_back);
         TextView myguanzhu_title=findViewById(R.id.myguanzhu_title);
        myguanzhu_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        queryALLguanzhu();
    }
    @Override
    public void onBackPressed(){
        finish();
    }
    /**
     * 查询我所有关注
     */
    private void queryALLguanzhu(){
        openLoading();
        if (BmobUser.isLogin()) {
            BmobQuery<User> query = new BmobQuery<User>();
            // 用此方式可以构造一个BmobPointer对象。只需要设置objectId就行

            query.addWhereRelatedTo("friend", new BmobPointer(user));
            //包含作者信息
            query.include("user,author");
            query.findObjects(new FindListener<User>() {
                @Override
                public void done(List<User> object, BmobException e) {
                    if (e == null) {
                        Log.w("aaaa", "我的主页关注个数"+object.size());
                        if (object.size()>0){
                            FriendAapter adapter = new FriendAapter(Myguanzhu.this,object,"yes");//NewAdapterDAO为自定义适配器
                            lsitnew.setAdapter(adapter);
                        }
                        closeLoading();
                    } else {
                        Log.e("aaaa","我的主页友关注失败"+ e.getMessage());
                        closeLoading();
                    }
                }
            });
        } else {
            Log.w("aaaa", "请先登录！");
        }
    }


    //网络加载
    private  void openLoading(){
        shapeLoadingDialog = new ShapeLoadingDialog.Builder(Myguanzhu.this)
                .loadText("加载中...")
                .build();
        shapeLoadingDialog.show();
    }
    //关闭网络加载
    private  void closeLoading(){
        shapeLoadingDialog.dismiss();
    }


}
